/**
 * Created by Jone on 2016/7/24.
 */

App.config(['$stateProvider', '$locationProvider', '$urlRouterProvider', 'RouteHelpersProvider',
    function ($stateProvider, $locationProvider, $urlRouterProvider, helper) {
        $stateProvider
            .state('app-api.member_create', {
                url: '/group/create',
                title: 'Single View',
                controller: 'GroupController',
                templateUrl: helper.basepath('group/create.html'),
                resolve: helper.resolveFor( 'toaster', 'ngDialog')
            })
            .state('app-api.member_list', {
                url: '/member/list',
                title: 'member list',
                controller: 'MemberController',
                templateUrl: helper.basepath('member/list.html'),
                resolve: helper.resolveFor( 'toaster', 'ngDialog')
            })
    }]);


App.controller('MemberController', function ($scope, $rootScope, $cookieStore, $http, $location, $window, $state, $stateParams, toaster) {

    $scope.create = function() {
        $scope.member.projectId = $cookieStore.get("project")._id;
        $http.post("/projects/" + $scope.member.projectId + "/users", $scope.member)
            .success(function (response) {
                if (response.code == 0) {
                    toaster.pop('success', '提示', '创建成功');
                    $scope.list();
                    $scope.member = {};
                }else{
                    toaster.pop('error', '提示',  response.msg);
                }
            }).error(function(data) {
                toaster.pop('danger', '提示', '创建失败' + JSON.stringify(data));
            });
    }

    $scope.delete = function(group) {
        $http.delete("/groups/" + group._id)
            .success(function (response) {
                if (response.code == 0) {
                    toaster.pop('success', '提示', '删除成功');
                    $http.get("/projects/" + $cookieStore.get("project")._id + "/groups")
                        .success(function (response) {
                            $scope.groups = response.data;
                        })
                        .error(function(data) {

                        });
                }
            });
    }

    $scope.list = function() {
        console.log(JSON.stringify($cookieStore.get("project")));
        $scope.project = $cookieStore.get("project");
        $http.get("/projects/" + $cookieStore.get("project")._id + "/users")
            .success(function (response) {
                $scope.members = response.data;
            })
            .error(function(data) {
            });
    }
});